/**
 * 路由配置文件 V2 - 使用更清晰的配置方式
 * 
 * @file frontend/src/routes/index-v2.tsx
 * @description 使用对象配置的方式，避免 JSX 语法，更加清晰易读
 * @author 类小脑计算平台开发团队
 * @date 2025-11-15
 */

import React, { lazy, Suspense, ComponentType } from 'react';
import { RouteObject, Navigate } from 'react-router-dom';

// ============================================================================
// 加载中组件
// ============================================================================

const LoadingFallback: React.FC = () => (
  <div style={{
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    height: '100vh',
    fontSize: '18px',
    color: '#666'
  }}>
    加载中...
  </div>
);

// ============================================================================
// 路由工厂函数 - 更清晰的方式
// ============================================================================

/**
 * 创建懒加载路由
 * @param path 路由路径
 * @param componentPath 组件路径（相对于 src 目录）
 * @returns 路由对象
 */
function createLazyRoute(path: string, componentPath: string): RouteObject {
  const Component = lazy(() => import(`../${componentPath}`));
  
  return {
    path,
    element: (
      <Suspense fallback={<LoadingFallback />}>
        <Component />
      </Suspense>
    ),
  };
}

/**
 * 创建重定向路由
 * @param from 源路径
 * @param to 目标路径
 * @returns 路由对象
 */
function createRedirectRoute(from: string, to: string): RouteObject {
  return {
    path: from,
    element: <Navigate to={to} replace />,
  };
}

// ============================================================================
// 路由配置 - 使用对象数组，更加清晰
// ============================================================================

/**
 * 主要页面路由配置
 * 使用对象数组的方式，每个对象包含路径和组件路径
 */
const mainRoutesConfig = [
  // 首页和主配置
  { path: '/', component: 'pages/Welcome' },
  { path: '/cell-config', component: 'pages/Main' },
  
  // 模型定义
  { path: '/populations', component: 'pages/Populations' },
  { path: '/syn-mechs', component: 'pages/SynMechs' },
  { path: '/conn-rules', component: 'pages/ConnRules' },
  { path: '/subcell-conn-rules', component: 'pages/SubcellConnRules' },
  { path: '/stim-sources', component: 'pages/StimSources' },
  { path: '/stim-targets', component: 'pages/StimTargets' },
  
  // 仿真配置
  { path: '/simulation-config-general', component: 'pages/SimulationConfigGeneral' },
  { path: '/simulation-config-record', component: 'pages/SimulationConfigRecord' },
  { path: '/simulation-config-network', component: 'pages/SimulationConfigNetwork' },
  { path: '/simulation-config-network-v2', component: 'pages/SimulationConfigNetworkV2' },
  { path: '/simulation-config-network-editor', component: 'pages/SimulationConfigNetworkEditor' },
  { path: '/simulation-config-save', component: 'pages/SimulationConfigSave' },
  { path: '/simulation-config-save-v2', component: 'pages/SimulationConfigSaveV2' },
  
  // 绘图设置
  { path: '/plot-settings-raster', component: 'pages/PlotSettingsRaster' },
  { path: '/plot-settings-trajectory', component: 'pages/PlotSettingsTrajectory' },
  { path: '/plot-settings-lfp', component: 'pages/PlotSettingsLFP' },
  
  // 反应扩散
  { path: '/reaction-diffusion-region', component: 'pages/ReactionDiffusionRegion' },
  { path: '/reaction-diffusion-empty', component: 'pages/ReactionDiffusionEmpty' },
  
  // 导入功能
  { path: '/import-neuroml', component: 'pages/ImportNeuroML' },
  { path: '/import-python', component: 'pages/ImportPythonDialog' },
  
  // 实验管理
  { path: '/experiment-manager', component: 'pages/ExperimentManager' },
  
  // 对话框
  { path: '/select-folder', component: 'pages/SelectFolderDialog' },
  { path: '/open-json', component: 'pages/OpenJSONDialog' },
  { path: '/save-json', component: 'pages/SaveJSONDialog' },
  { path: '/save-json-options', component: 'pages/SaveJSONDialogOptions' },
  { path: '/delete-confirm', component: 'pages/DeleteConfirmDialog' },
  
  // 特殊页面
  { path: '/netpyne', component: 'components/NetPyNE/NetPyNE' },
  { path: '/geppetto-test', component: 'pages/GeppettoTest' },
];

/**
 * 兼容性路由配置（重定向）
 */
const legacyRoutesConfig = [
  { from: '/synaptic-mechanisms', to: '/syn-mechs' },
  { from: '/connectivity-rules', to: '/conn-rules' },
  { from: '/subcellular-connectivity', to: '/subcell-conn-rules' },
  { from: '/stimulation-sources', to: '/stim-sources' },
  { from: '/stimulation-targets', to: '/stim-targets' },
  { from: '/plot-settings-traces', to: '/plot-settings-trajectory' },
  { from: '/reaction-diffusion-extracellular', to: '/reaction-diffusion-empty' },
];

/**
 * Frame 路由配置（重定向）
 */
const frameRoutesConfig = [
  { from: '/frame/8', to: '/cell-config' },
  { from: '/frame/10', to: '/simulation-config-general' },
  { from: '/frame/13', to: '/plot-settings-raster' },
  { from: '/frame/14', to: '/plot-settings-trajectory' },
  { from: '/frame/15', to: '/reaction-diffusion-region' },
  { from: '/frame/16', to: '/plot-settings-lfp' },
  { from: '/frame/17', to: '/reaction-diffusion-empty' },
  { from: '/frame/18', to: '/simulation-config-record' },
  { from: '/frame/19', to: '/simulation-config-network' },
  { from: '/frame/20', to: '/simulation-config-save' },
  { from: '/frame/21', to: '/simulation-config-network-v2' },
  { from: '/frame/22', to: '/simulation-config-save-v2' },
  { from: '/frame/23', to: '/import-neuroml' },
  { from: '/frame/24', to: '/simulation-config-network-editor' },
  { from: '/frame/25', to: '/experiment-manager' },
  { from: '/frame/26', to: '/select-folder' },
  { from: '/frame/27', to: '/open-json' },
  { from: '/frame/28', to: '/save-json' },
  { from: '/frame/29', to: '/delete-confirm' },
  { from: '/frame/30', to: '/save-json-options' },
  { from: '/frame/31', to: '/import-python' },
];

// ============================================================================
// 生成路由对象
// ============================================================================

/**
 * 将配置转换为路由对象
 */
const mainRoutes: RouteObject[] = mainRoutesConfig.map(config => 
  createLazyRoute(config.path, config.component)
);

const legacyRoutes: RouteObject[] = legacyRoutesConfig.map(config =>
  createRedirectRoute(config.from, config.to)
);

const frameRoutes: RouteObject[] = frameRoutesConfig.map(config =>
  createRedirectRoute(config.from, config.to)
);

/**
 * 合并所有路由
 */
const routes: RouteObject[] = [
  ...mainRoutes,
  ...legacyRoutes,
  ...frameRoutes,
];

export default routes;

